package com.weakteam.orderonline.adminconsole.gwt.client.design;

import com.google.gwt.cell.client.DateCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.weakteam.orderonline.adminconsole.gwt.client.domain.Order;
import com.weakteam.orderonline.adminconsole.gwt.client.widget.OrdersList;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * Created by Max on 03.04.2014.
 */
public class OrdersPage extends Composite {
    interface OrdersPageUiBinder extends UiBinder<Widget, OrdersPage> { }
    private static OrdersPageUiBinder uiBinder = GWT.create(OrdersPageUiBinder.class);

    private static OrdersPage instance = null;
    public static OrdersPage getInstance() {
        if (instance == null) {
            instance = new OrdersPage();
        }
        return instance;
    }

    @UiField  protected FlowPanel contentPanel;
//    protected CellTable<Order> ordersBoard;

    @UiFactory
    Header makeHeader() { // method name is insignificant
        return Header.getInstance();
    }


    private OrdersPage() {
        initWidget(uiBinder.createAndBindUi(this));
        CellTable<Order> ordersBoard = new CellTable<>();


        TextColumn<Order> orderColumn = new TextColumn<Order>() {
            @Override
            public String getValue(Order object) {
                return "#"+object.getId().toString();
            }
        };
        ordersBoard.addColumn(orderColumn, "#");

        TextColumn<Order> createdColumn = new TextColumn<Order>() {
            @Override
            public String getValue(Order object) {
                return object.getCreated();
            }
        };
        ordersBoard.addColumn(createdColumn, "Создан");

        TextColumn<Order> placeColumn = new TextColumn<Order>() {
            @Override
            public String getValue(Order object) {
                return object.getPlaceName();
            }
        };
        ordersBoard.addColumn(placeColumn, "Стол");

        TextColumn<Order> orderContentColumn = new TextColumn<Order>() {
            @Override
            public String getValue(Order object) {
                return object.getContent();
            }
        };
        ordersBoard.addColumn(orderContentColumn, "Заказ");

        TextColumn<Order> statusColumn = new TextColumn<Order>() {
            @Override
            public String getValue(Order object) {
                return object.getStatus();
            }
        };
        ordersBoard.addColumn(statusColumn, "Статус");

//        final SingleSelectionModel<Order> selectionModel = new SingleSelectionModel<Order>();
//        ordersBoard.setSelectionModel(selectionModel);
//        selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
//            public void onSelectionChange(SelectionChangeEvent event) {
//                Order selected = selectionModel.getSelectedObject();
//                if (selected != null) {
//                    Window.alert("You selected: " + selected.getId());
//                }
//            }
//        });

        OrdersList ordersList = new OrdersList();
        ordersList.addDataDisplay(ordersBoard);
        contentPanel.add(ordersBoard);
    }

}
